<template>
  <div>
    <div style="margin-bottom: 10px">

    </div>

    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
      <!--      <el-input style="width: 200px" placeholder="请输入邮箱" suffix-icon="el-icon-message" class="ml-5"-->
      <!--                v-model="email"></el-input>-->
      <!--      <el-input style="width: 200px" placeholder="请输入地址" suffix-icon="el-icon-position" class="ml-5"-->
      <!--                v-model="address"></el-input>-->
      <!--      <el-input style="width: 200px" placeholder="请输入手机号" suffix-icon="el-icon-phone" class="ml-5"-->
      <!--                v-model="phone"></el-input>-->
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAddUser">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='好的'
          cancel-button-text='不用了'
          icon="el-icon-info"
          icon-color="red"
          title="确定删除吗？"
          @confirm="deleteMore"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
      <!--      <el-upload-->
      <!--          :limit="1"-->
      <!--          action="http://localhost:9090/role/import"-->
      <!--          style="display: inline-block"-->
      <!--          :show-file-list="false"-->
      <!--          accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"-->
      <!--          :on-success="handleExcelUploadSuccess"-->
      <!--      >-->
      <!--        <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>-->
      <!--      </el-upload>-->

      <!--      <el-button type="primary" @click="exportFile" class="ml-5">导出 <i class="el-icon-top"></i></el-button>-->
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg" style="font-size: 15px"
              @selection-change="handleSelectionChangeDeleteMore"
              row-key="id"
              default-expand-all
    >

      <el-table-column type="selection"></el-table-column>
      <el-table-column prop="id" label="序号" width="100"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="path" label="路径"></el-table-column>
      <el-table-column prop="pagePath" label="页面路径"></el-table-column>
      <el-table-column prop="icon" label="图标" class-name="fontSize18" align="center" label-class-name="fontSize12">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述"></el-table-column>

      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleAddChildrenMenu(scope.row.id)" v-if="!scope.row.pid && !scope.row.path">新增子菜单 <i
              class="el-icon-plus"></i>
          </el-button>
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='好的'
              cancel-button-text='不用了'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="handleDelete(scope.row.id)">
            >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i>
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>


    <!--对话框弹出-->
    <el-dialog title="菜单信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="120px">
        <el-form-item label="名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="form.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="页面路径">
          <el-input v-model="form.pagePath" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <template slot-scope="scope">
            <el-select clearable v-model="form.icon" placeholder="请选择">
              <el-option v-for="item in options" :label="item.name" :key="item.name" :value="item.value">
                <i :class="item.value"/> {{ item.name }}
              </el-option>
            </el-select>
          </template>
          <!--          <el-input v-model="form.icon" autocomplete="off"></el-input>-->
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveUser">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {serverIp} from "../../public/config";

export default {
  name: "User",
  data() {
    return {
      serverIp: serverIp,
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      name: '',
      form: [],//表单数据
      options: [],//下拉框数据
      multipleSelection: [],//多选
      dialogFormVisible: false,//对话框
      headerBg: 'headerBg',
    }
  },
  created() {
    this.load();
  },
  methods: {
    // 加载数据
    load() {
      this.request.get("/menu", {
        // 参数
        params: {
          name: this.name,
        }
      }).then(res => {
        this.tableData = res.data
      })
    },
    //分页方法实现
    //页码
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    //页码
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    //重置搜索按钮
    reset() {
      this.name = ''
      // this.phone = ''
      // this.email = ''
      // this.address = ''
      this.load()
    },
    //新增用户信息
    handleAddUser() {
      this.form = {
        username: '',
        nickname: '',
        email: '',
        address: '',
        phone: '',
      }
      this.dialogFormVisible = true;
      //请求菜单图标列表
      this.request.get("/dict/icon").then(res => {
        this.options = res.data
      })
    },
    //保存用户信息
    saveUser() {
      this.request.post("/menu/saveOrUpdate", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success('保存成功')
          this.dialogFormVisible = false;
          this.load()
        } else {
          this.$message.error('保存失败')
        }
      })
    },
    //编辑用户信息
    handleEdit(row) {
      this.dialogFormVisible = true;
      this.form = JSON.parse(JSON.stringify(row))  //可以解决编辑未确认就改变的问题
      //请求菜单图标列表
      this.request.get("/dict/icon").then(res => {
        this.options = res.data
      })
    },
    //删除用户信息
    handleDelete(id) {
      this.request.delete("/menu/delOne/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success('删除成功')
          this.load()
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    //批量删除
    handleSelectionChangeDeleteMore(val) {
      console.log(val)
      this.multipleSelection = val
    },
    deleteMore() {
      let ids = this.multipleSelection.map(item => item.id)//[{},{}] => [1,2,3]
      this.request.post("/menu/del/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success('删除成功')
          this.load()
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    //文件导出操作
    exportFile() {
      window.open(`http://${serverIp}:9090/menu/export`)
    },
    //导入文件
    handleExcelUploadSuccess() {
      this.$message.success('导入成功')
      this.load()
    },
    //添加子菜单
    handleAddChildrenMenu(pid) {
      this.dialogFormVisible = true;
      this.form = {}
      if (pid) {
        this.form.pid = pid
      }
      //请求菜单图标列表
      this.request.get("/dict/icon").then(res => {
        this.options = res.data
      })
    },
  }
}
</script>

<style>
.headerBg {
  background: #eee !important;
}
.fontSize18 {
  font-size: 18px;
}
.fontSize12 {
  font-size: 12px;
}

</style>